home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / diskutil / mutate.lzh / MUTATE.C < prev    next >
C/C++ Source or Header  |  1990-05-13  |  16KB  |  807 lines

  1. #include <aes.h>
  2. #include <linea.h>
  3. #include <tos.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ext.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <mydefs.h>
  10. #include "mutate.h"
  11.  
  12. char *pbase;
  13. long ramtop;
  14. long rambot;
  15. int    changed;
  16. int cdrive;
  17. int coffst;
  18.  
  19. #define MODEM 0
  20. #define MODEF 1
  21. #define MODED 2
  22.  
  23. struct
  24.     {
  25.     unt    bps;    /* bytes per sector        */
  26.     unt    spc;    /* sectors per cluster    */
  27.     unt    bpc;    /* bytes per cluster    */
  28.     unt    res;    /*                         */
  29.     unt    fat;    /* number of fats        */
  30.     unt    dir;    /* # of dir entries        */
  31.     unt    sec;    /* # of sectors / disk    */
  32.     unt    spf;    /* # of sectors / fat    */
  33.     unt    spt;    /* # of sectors / track    */
  34.     unt    sid;    /* # of sides            */
  35.     unt    hid;    /* # of hidden            */
  36.     unt    bpe;    /* bytes per fat entry    */
  37.     unt    cpf;    /* clusters per fat        */
  38.     unt cps;    /* clusters per system    */
  39.     unt cpd;    /* clusters per direct.    */
  40.     unt sps;    /* sectors per system    */
  41.     unt spd;    /* sectors per direct.    */
  42.     unt bad;    /* bad clusters on disk    */
  43.     unt dcl;    /* data clusters on disk*/
  44.     unt clu;    /* total clusters/disk    */
  45.     } boot;
  46.  
  47. /***********
  48.     blink cursor at x,y
  49. ***********/
  50. long cblink(int x, int y, int a)
  51.     {
  52.     long cx,cy;
  53.     register int    i;
  54.     
  55.     if (!a)
  56.         cx=(long)x+13+(coffst==0)*(x/4);
  57.     else
  58.         cx=(long)x/2+53;
  59.     cy=(long)y*16+108-coffst;
  60.     while(!Cconis())
  61.         {
  62.         for(i=0;i<16;i++)
  63.             *(pbase+cx+((cy+i)*80))^=0xff;
  64.         if (!Cconis())
  65.             delay(100);
  66.         for(i=0;i<16;i++)
  67.             *(pbase+cx+((cy+i)*80))^=0xff;
  68.         if (!Cconis())
  69.             delay(100);
  70.         }
  71.     return(Crawcin());
  72.     }
  73.  
  74. /***********
  75.     print at x,y
  76. ***********/
  77. void printxy(int x, int y, int a, uch c)
  78.     {
  79.     long cx,cy;
  80.     register int i;
  81.  
  82.     Bconout(2,27);
  83.     Bconout(2,'H');
  84.     Bconout(2,(int)c);
  85.     if (!a)
  86.         cx=(long)x+13+(x/4);
  87.     else
  88.         cx=(long)x/2+53;
  89.     cy=(long)y*16+108;
  90.     for(i=0;i<16;i++)
  91.         {
  92.         *(pbase+cx+((cy+i)*80))=*(pbase+i*80);
  93.         *(pbase+i*80)=0;
  94.         }
  95.     }
  96.  
  97. /***********
  98.     Get a long hex input
  99. ***********/
  100. long getlong(int x, int y, long old, int box, int item)
  101.     {
  102.     int c;
  103.     long k,l;
  104.     int p,done;
  105.     
  106.     l=0;
  107.     p=0;
  108.     done=0;
  109.     while(p<7&&!done)
  110.         {
  111.         sprintf(rs_object[rs_trloc[box]+item].ob_spec.free_string,"%lx",l);
  112.         dial_draw(box,item);
  113.         k=cblink(x+p,y,0);
  114.         c=(int)k;
  115.         if ((c==0)||(k==0xE0008L))
  116.             {
  117.             if (((k==0x4d0000L)||(k==0xE0008L))&&(p>0))
  118.                 {
  119.                 p-=1;
  120.                 l=l>>4;
  121.                 }
  122.             else
  123.                 done=1;
  124.             }
  125.         else if ((done=(c==13))==0)
  126.             {
  127.             if (islower(c)) c=toupper(c);
  128.             if ((c<'0')||(c>'F')||((c>'9')&&(c<'A')))
  129.                 printf("\007");
  130.             else
  131.                 {
  132.                 if (c>'9')
  133.                     c=c-55;
  134.                 else
  135.                     c=c-48;
  136.                 l=((l<<4)|c);
  137.                 ++p;
  138.                 }
  139.             }
  140.         }
  141.     if (c==13)
  142.         return(l);
  143.     else
  144.         return(old);
  145.     }
  146.  
  147. /***********
  148.     Supervisor Peek
  149. ***********/
  150. long speekl;
  151. long speekv;
  152. void speek(void)
  153.     {
  154.     speekv=*(long *)speekl;
  155.     }
  156.  
  157. /***********
  158.     Supervisor Poke
  159. ***********/
  160. void spoke(void)
  161.     {
  162.     *(long *)speekl=speekv;
  163.     }
  164.  
  165. /***********
  166.     Peek a long word
  167. ***********/
  168. long peek(long loc)
  169.     {
  170.     if ((loc<ramtop)&&(loc>rambot))
  171.         return(*(long *)loc);
  172.     else
  173.         {
  174.         speekl=loc;
  175.         Supexec(speek);
  176.         return(speekv);
  177.         }
  178.     }
  179.  
  180. /**********
  181.     Poke a long word
  182. **********/
  183. void poke(long loc, long val)
  184.     {
  185.     if ((loc<ramtop)&&(loc>rambot))
  186.         *(long *)loc=val;
  187.     else
  188.         {
  189.         speekl=loc;
  190.         speekv=val;
  191.         Supexec(spoke);
  192.         }
  193.     }
  194.  
  195. /***********
  196.     Getfile
  197. ***********/
  198. int getfile(uch **buf, long *size)
  199.     {
  200.     int        c,fd;
  201.     long    l;
  202.     static    char *f="FILENAME.EXT";
  203.     static    char *p="A:\\*.*\0                                                 ";
  204.     char    q[128];
  205.     uch        *b;
  206.     
  207.     *p='A'+(char)Dgetdrv();
  208.     fsel_exinput(p,f,&c,"Choose a file to MUTATE!");
  209.     if (c)
  210.         {
  211.         Dsetdrv((int)*p-65);
  212.         strcpy(q,p+2);
  213.         strcpy(strrchr(q,'\\')+1,f);
  214.         if (Fsfirst(q,0)>=0)
  215.             {
  216.             Fgetdta();
  217.             fd=Fopen(q,READ);
  218.             l=filelength(fd);
  219.             if ((b=malloc(l))!=NULL)
  220.                 {
  221.                 if (*buf!=NULL)
  222.                     free(*buf);
  223.                 *buf=b;
  224.                 strcpy(rs_object[rs_trloc[MAIN]+MAINFNAM].ob_spec.free_string,f);
  225.                 dial_draw(MAIN,MAINFNAM);
  226.                 Fread(fd,l,*buf);
  227.                 *size=l;
  228.                 Fclose(fd);
  229.                 return(1);
  230.                 }
  231.             else
  232.                 {
  233.                 Fclose(fd);
  234.                 return(0);
  235.                 }    
  236.             }
  237.         else    
  238.             return(0);
  239.         }
  240.     else
  241.         return(0);
  242.     }
  243.  
  244.  
  245. /***********
  246.     Putfile
  247. ***********/
  248. int putfile(uch *buf, long size)
  249.     {
  250.     int        c,fd;
  251.     static    char *f="FILENAME.EXT";
  252.     static    char *p="A:\\*.*\0                                                 ";
  253.     char    q[128];
  254.     
  255.     *p='A'+(char)Dgetdrv();
  256.     fsel_exinput(p,f,&c,"Choose a file to save the MUTATION!");
  257.     if (c)
  258.         {
  259.         Dsetdrv((int)*p-65);
  260.         strcpy(q,p+2);
  261.         strcpy(strrchr(q,'\\')+1,f);
  262.         fd=1;
  263.         if (Fsfirst(q,0)>=0)
  264.             {
  265.             Fgetdta();
  266.             fd=mess2("Do you wish to","overwrite?","NO","YES",1);
  267.             }
  268.         if (fd)
  269.             {
  270.             Fdelete(f);
  271.             fd=Fcreate(f,0);
  272.             Fclose(fd);
  273.             fd=Fopen(q,WRITE);
  274.             Fwrite(fd,size,buf);
  275.             Fclose(fd);
  276.             return(1);
  277.             }
  278.         else
  279.             return(putfile(buf,size));
  280.         }
  281.     else
  282.         return(0);
  283.     }
  284.  
  285. /****************************
  286.     Check in disk & set info
  287. ****************************/
  288. void dmapdriv(int drive)
  289.     {
  290.     long     e;
  291.     unt        *buf;
  292.  
  293.     buf = (unt *)malloc(0x2000);
  294.     Dsetdrv(drive);
  295.     if (Mediach(drive))
  296.         Getbpb(drive);
  297.     if ((e=Rwabs(READ+2,buf,1,0,drive))!=0)
  298.         {
  299.         mess("Boot read error!");
  300.         messl(e);
  301.         }
  302.     else
  303.         {
  304.         boot.bps = (buf[5]&0x00ff)+(buf[6]&0xff00);
  305.         boot.spc = (buf[6]&0x00ff);
  306.         boot.res = ((buf[7]&0x00ff)<<8)+((buf[7]&0xff00)>>8);
  307.         boot.fat = (buf[8]&0xff00)>>8;
  308.         boot.dir = (buf[8]&0x00ff)+(buf[9]&0xff00);
  309.         boot.sec = (buf[9]&0x00ff)+(buf[10]&0xff00);
  310.         boot.spf = ((buf[11]&0x00ff)<<8)+((buf[11]&0xff00)>>8);
  311.         boot.spt = ((buf[12]&0x00ff)<<8)+((buf[12]&0xff00)>>8);
  312.         boot.sid = ((buf[13]&0x00ff)<<8)+((buf[13]&0xff00)>>8);
  313.         boot.hid = ((buf[14]&0x00ff)<<8)+((buf[14]&0xff00)>>8);
  314.         boot.bpc = (boot.bps * boot.spc);
  315.         boot.spd = (boot.dir / (boot.bps/0x20) * (boot.bps/0x200));
  316.         boot.cpd =((boot.spd +1)/ boot.spc);
  317.         boot.cpf = (boot.spf / boot.spc) * boot.fat * (boot.bps/0x200);
  318.         boot.cps = (boot.cpf + boot.cpd);
  319.         boot.sps =((boot.spf * boot.fat) + boot.spd);
  320.         boot.dcl =((boot.sec-boot.sps)/boot.spc);
  321.         boot.clu = (boot.dcl + boot.cps);
  322.         boot.bpe = 4;
  323.         if (drive < 2) boot.bpe = 3;
  324.         }
  325.     free(buf);
  326.     }
  327.  
  328. /***********
  329.     Check for Update
  330. ***********/
  331. void putbuf(int mode, uch *buf, long boff, long bufsize)
  332.     { 
  333.     long l,m;
  334.     
  335.     if (changed)
  336.         {
  337.         changed=0;
  338.         if (mess2("Do you wish to","make changes permanent","NO","YES",0))
  339.             {
  340.             switch(mode)
  341.                 {
  342.                 case MODEM:
  343.                     for(l=0L;(l<bufsize)&&(l+boff<ramtop);l+=4)
  344.                         poke(l+boff,*((long *)(buf+l)));
  345.                     break;
  346.                 case MODEF:
  347.                     putfile(buf,bufsize);
  348.                     break;
  349.                 case MODED:
  350.                     dmapdriv(cdrive);
  351.                     l=bufsize/boot.bps;
  352.                     m=boff/boot.bps;
  353.                     if (Rwabs(WRITE+2,buf,(int)l,(int)m,cdrive)<0)
  354.                         mess("CAREFUL! Write Error");
  355.                     break;
  356.                 }
  357.             }
  358.         }
  359.     }
  360.  
  361. /***********
  362.     Fill the buffer with data
  363. ***********/
  364. void fillbuf(int mode, uch *buf, long boff, long bufsize)
  365.     {
  366.     long    l,m;
  367.  
  368.     putbuf(mode,buf,boff,bufsize);
  369.     switch (mode)
  370.         {
  371.         case MODEM:
  372.             for(l=0L;l<bufsize;l+=4)
  373.                 *((long *)(buf+l))=peek(l+boff);
  374.             break;
  375.         case MODEF:
  376.             break;
  377.         case MODED:
  378.             dmapdriv(cdrive);
  379.             l=bufsize/boot.bps;
  380.             m=boff/boot.bps;
  381.             if (Rwabs(READ+2,buf,(int)l,(int)m,cdrive)<0)
  382.                 mess("CAREFUL! Read Error");
  383.             break;
  384.         }
  385.     }
  386.  
  387. /***********
  388.     Update the window
  389. ***********/
  390. void update(long offset, long boff, long bufsize, uch *buf)
  391.     {
  392.     int        i,j;
  393.     long    m;
  394.     long    l;
  395.     uch        c;
  396.     char    s[5];
  397.     
  398.     for(i=0,l=0L;(l<0xa0L)&&(l<bufsize-offset);l+=0x10,i++)
  399.         {
  400.         sprintf(rs_object[rs_trloc[MAIN]+MAINSTR0+i].ob_spec.tedinfo->te_ptext,
  401.             "%6lx",l+offset);
  402.         *(rs_object[rs_trloc[MAIN]+MAINSTR0+i].ob_spec.tedinfo->te_ptext+6)=':';
  403.         for(m=0;(m<16)&&(l+m<bufsize-offset);m++)
  404.             {
  405.             c=*(buf+l+m);
  406.             sprintf(s,"%4x",(unt)c);
  407.             if (*(s+2)==' ')
  408.                 *(s+2)='0';
  409.             if (c==0)
  410.                 c=0x2e;
  411.             *(rs_object[rs_trloc[MAIN]+MAINSTR0+i].ob_spec.tedinfo->te_ptext+m+47)=c;
  412.             for(j=0;j<2;j++)
  413.                 *(rs_object[rs_trloc[MAIN]+MAINSTR0+i].ob_spec.tedinfo->te_ptext+j+m*2+m/2+7)=s[j+2];
  414.             }
  415.         }
  416.     sprintf(rs_object[rs_trloc[MAIN]+MAINOFFS].ob_